<template>
  <div class="app-container">
    <el-form :model="queryParams" :inline="true">
        <el-form-item label="抽中者昵称" >
            <el-input
                v-model="queryParams.nickName"
                placeholder="请输入"
                clearable
                @keyup.enter="handleQuery"
            />
        </el-form-item>
        <el-form-item label="抽中者ID" >
            <el-input
                v-model="queryParams.userId"
                placeholder="请输入"
                clearable
                @keyup.enter="handleQuery"
            />
        </el-form-item>
        <el-form-item label="开启状态" >
            <el-select
                class="query-el-input-width"
                v-model="queryParams.openStatus"
                placeholder="请选择"
                clearable
                filterable
                style="width: 214px"
            >
                <el-option
                v-for="item in ['开启','关闭']"
                :key="item"
                :value="item"
                :label="item"
                class="query-el-input-width"
                />
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="addhandle">新增抽奖</el-button >
         <el-button type="primary" @click="setTopica">今日话题设置</el-button>
      </el-col>
    </el-row>
    <el-table  class="newClass" v-loading="loading" :data="localLifeTableList">
        <el-table-column label="期数" align="center" prop="id" />
        <el-table-column label="抽中者昵称" align="center" prop="nickName" />
        <el-table-column label="抽中者ID" align="center" prop="userId" />
        <el-table-column label="抽中时间" align="center" prop="luckyTime" />
        <el-table-column label="奖品" align="center" prop="prize" />
        <el-table-column label="联系方式" align="center" prop="personNum" />
        <el-table-column label="城市" align="center" prop="city" />
        <el-table-column label="收货地址" align="center" prop="shippingAddress" />
        <el-table-column label="发货状态" align="center" prop="shippingStatus" />
        <el-table-column label="备注" align="center" prop="remake" />
        <el-table-column label="开启状态" align="center" prop="openStatus" />
        <el-table-column label="当前次数" align="center" prop="currentNum" />
        <el-table-column label="中奖次数" align="center" prop="winningNum" />
        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
            <template #default="scope">
                <el-button link type="primary" icon="" @click="handleUpdate(scope.row)">编辑</el-button>
            </template>
        </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="新增抽奖" v-model="open1" width="700">
        <el-form :model="form1" label-width="100px">
            <el-form-item label="是否开启" prop="openStatus">
               <el-radio-group v-model="form1.openStatus">
                    <el-radio label="开启">开启</el-radio>
                    <el-radio label="关闭">关闭</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="点击次数" prop="winningNum">
                <el-input
                v-model="form1.winningNum"
                placeholder="点击次数"
                type="text"
                style="width: 215px"
                />
            </el-form-item>
            <el-form-item label="中奖人ID" prop="userId">
                <el-input
                v-model="form1.userId"
                placeholder="中奖人ID"
                type="text"
                style="width: 215px"
                />
            </el-form-item>
            <el-form-item label="奖品名称" prop="prize">
                <el-input
                v-model="form1.prize"
                placeholder="奖品名称"
                type="text"
                style="width: 215px"
                />
            </el-form-item>
            <el-form-item label="抽奖图片" prop="lotteryImg">
                <image-uploads v-model="form1.lotteryImg"></image-uploads>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
            <el-button :loading="buttonLoading1" :disabled="buttonLoading1" type="primary" @click="submitForm1">发布</el-button>
            <el-button @click="cancel">取 消</el-button>
            </div>
        </template>
    </el-dialog>
    <el-dialog title="今日话题设置" v-model="open2" width="700">
        <el-form :model="form2" label-width="100px">
            <el-form-item label="跳转话题ID" prop="topicId">
                <el-select
                    class="query-el-input-width"
                    v-model="form2.topicId"
                    placeholder="请选择"
                    clearable
                    filterable
                    style="width: 214px"
                >
                    <el-option
                        v-for="item in topicList"
                        :key="item.id"
                        :value="item.id"
                        :label="item.id"
                        class="query-el-input-width"
                    />
                </el-select>
                (话题名称：{{ topicName }})
            </el-form-item>
            <el-form-item label="话题图片" prop="topicImg">
                <image-uploads v-model="form2.topicImg"></image-uploads>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
            <el-button :loading="buttonLoading2" :disabled="buttonLoading2" type="primary" @click="submitForm2">确定</el-button>
            <el-button @click="cancel">取 消</el-button>
            </div>
        </template>
    </el-dialog>
  </div>
</template>

<script setup name="LotterySettings">
import {ref} from "vue";
import {currentTopicsDateils,upCurrentTopics,upScanLucky,addScanLucky,scanLuckyList,newTopicList} from "@/api/business/newMiniProgram";
const localLifeTableList = ref([]);
const loading = ref(true);
const buttonLoading1 = ref(false);
const buttonLoading2 = ref(false);
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
})
const { proxy } = getCurrentInstance();
const open1 = ref(false);
const open2 = ref(false);
const form1 = ref({});
const form2 = ref({});
const total = ref(0);
const topicList = ref([]);
const topicName = ref('');
watchEffect(()=>{
    if(form2.value.topicId&&topicList.value.length>0){
        topicList.value.forEach(element => {
            if(form2.value.topicId == element.id){
                topicName.value = element.topicName
            }
        });
    }
})
function addhandle(){
    form1.value = {}
    open1.value = true
}
function handleUpdate(e){
    form1.value = JSON.parse(JSON.stringify(e))
    open1.value = true
}
function submitForm1(){
    buttonLoading1.value = true
    if(form1.value.id){
        upScanLucky(form1.value).then(res=>{
            if(res.code==200){
                proxy.$modal.msgSuccess("修改成功");
                getList()
                open1.value = false
            }
        }).finally(()=>{
            buttonLoading1.value = false
        })
    }else{
        addScanLucky(form1.value).then(res=>{
            if(res.code==200){
                proxy.$modal.msgSuccess("新增成功");
                getList()
                open1.value = false
            }
        }).finally(()=>{
            buttonLoading1.value = false
        })
    }
   
}
function submitForm2(){
    buttonLoading2.value = true
    upCurrentTopics(form2.value).then(res=>{
        proxy.$modal.msgSuccess("成功");
        open2.value = false
    }).finally(()=>{
        buttonLoading2.value = false
    })
}
function setTopica(){
    currentTopicsDateils(1).then(res=>{
        form2.value = res.data
        open2.value = true
    })
}
/** 查询极客侠后台管理系统本地消耗列表 */
function getList() {
  loading.value = true;
  scanLuckyList(queryParams.value).then(response => {
    localLifeTableList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
function getList2() {
  newTopicList({
    pageNum: 1,
    pageSize: 500,
  }).then(response => {
    topicList.value = response.rows;
  });
}
function cancel(e) {
  open.value = false
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
getList();
getList2();
</script>

